﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_KhachHangThanThiet_KHTT_XemThongTinGioHang : System.Web.UI.Page
{
    float tong = 0;
    GlobalFunctions gf;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        
        //gia tri trong application
        //int RowPerPage = (int)Application["RowPerPage"];

        //gia tri trong web.config
  
        DataTable table = (DataTable)Session["GioHang"];
        gf = new GlobalFunctions();
        if (!this.IsPostBack)
        {
            if (gf.IsKHTT() == 1)
            {
                DataList_DanhSachSanPham.DataSource = table;
                DataList_DanhSachSanPham.DataBind();
            }
            else
            {
                DataList_DanhSachSanPham.DataSource = table;
                DataList_DanhSachSanPham.DataBind();
            }
             
                lblTong.Text = tong.ToString();
        }
    }
    protected void DataList_DanhSachSanPham_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (!IsPostBack)
        {
            DataListItem item = e.Item;
            if (((DataRowView)item.DataItem)!=null)
            {
                string soluong = ((DataRowView)item.DataItem).Row["SoLuong"].ToString();
                string thanhtien = ((DataRowView)item.DataItem).Row["TongTien"].ToString();
                string idsp = ((DataRowView)item.DataItem).Row["Id_SP"].ToString();
 
                //  TextBox textbox = (TextBox)item.FindControl("txtSoLuong");
                //textbox.Text = soluong;
                Label lable = (Label)item.FindControl("lblThanhTien");
                lable.Text = thanhtien;
                DataDSTableAdapters.SANPHAMTableAdapter spadapter = new DataDSTableAdapters.SANPHAMTableAdapter();
                DataDS.SANPHAMDataTable sptable = spadapter.SANPHAM_LayTheoIdSP(int.Parse(idsp));
                HyperLink hyper = (HyperLink)item.FindControl("HyperLink_PicLink");
                hyper.NavigateUrl = "../NguoiDung/NguoiDung_XemChiTietSanPham.aspx?MaSP=" + sptable[0]["MaSP"].ToString();
                Image img = (Image)item.FindControl("Image_hinhSP");
                img.ImageUrl = sptable[0]["HinhAnh"].ToString();
                Label tensp = (Label)item.FindControl("lblTen");
                tensp.Text = sptable[0]["TenSP"].ToString();
                Label lblsoluong = (Label)item.FindControl("lblSoLuong");
                lblsoluong.Text = "<input type='hidden' name='"+idsp+"txtSoLuong' value='" + sptable[0]["SoLuong"].ToString() + "' />" 
                    + "<input type='hidden' name='"+idsp+"txtSoLuong' value='" + sptable[0]["TenSP"].ToString() + "' />";
                Label giasp = (Label)item.FindControl("lblGia");
                giasp.Text = sptable[0]["GiaCu"].ToString();
                if (int.Parse(((DataRowView)item.DataItem).Row["Id_HinhThuc"].ToString()) == -1)
                {
                    TextBox textbox = (TextBox)item.FindControl("txtSoLuong");
                    textbox.Enabled = false;
                }
                Label lblkhuyenmai = (Label)item.FindControl("lblKhuyenMai");
                lblkhuyenmai.Text = "";
                if (int.Parse(((DataRowView)item.DataItem).Row["Id_HinhThuc"].ToString()) == 2)
                {
                    lblkhuyenmai.Text = "Giảm giá <b>" + ((DataRowView)item.DataItem).Row["GiaTri"].ToString() + " %</b>";
                }
                else
                {
                    if (int.Parse(((DataRowView)item.DataItem).Row["Id_HinhThuc"].ToString()) == 1)
                    {
                        int giatri = int.Parse(((DataRowView)item.DataItem).Row["GiaTri"].ToString());
                        DataDS.SANPHAMDataTable spkmtable = spadapter.SANPHAM_LayTheoIdSP(giatri);
                        string chuoi = "Tặng kèm sản phẩm:<br />";
                        chuoi +="<img width='50' height='50' src='" +spkmtable.Rows[0]["HinhAnh"].ToString().Replace("~", "/SMO")+"' />";
                        lblkhuyenmai.Text = chuoi;
                    }
                }
                if (((DataRowView)item.DataItem).Row["Is_KhuyenMai"].ToString() == "False")
                    tong += float.Parse(thanhtien);
            }
       } 
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        DataTable table = (DataTable)Session["GioHang"];
        int i;
        ArrayList arr = new ArrayList();
        for (i = 0; i < DataList_DanhSachSanPham.Items.Count; i++)
        {
            DataListItem item = DataList_DanhSachSanPham.Items[i];
            DataRow row = table.Rows[i];
            TextBox textbox = (TextBox)item.FindControl("txtSoLuong");
            int temp;
            if (int.TryParse(textbox.Text, out temp) == false || temp < 0)
            {
                continue;
            }
            Label lblid= (Label)item.FindControl("lblid");
            string[] value = Request.Form.GetValues(lblid.Text + "txtSoLuong");
            if(temp > int.Parse(value[0]))
            {
                arr.Add(value[1]);
                arr.Add(value[0]);
                continue;
            }
         
            row["SoLuong"] = textbox.Text;
            int soluong = int.Parse(row["SoLuong"].ToString());
            int giatri = int.Parse(row["GiaTri"].ToString());
            row["TongTien"] = soluong * float.Parse(row["DonGia"].ToString());
            if (int.Parse(row["Id_HinhThuc"].ToString()) == 2)
            {
                row["TongTien"] = soluong * (float.Parse(row["DonGia"].ToString())*(100-giatri)/100);
            }
            if (int.Parse(row["Id_HinhThuc"].ToString()) == 1)
            {
                int j = i + 1;
                DataRow rowkm = table.Rows[j];
                rowkm["SoLuong"] = row["SoLuong"];
                soluong = int.Parse(rowkm["SoLuong"].ToString());
                rowkm["TongTien"] = soluong * float.Parse(row["DonGia"].ToString());
                i = j;
            }
        }
        i = 0;
        while (i < table.Rows.Count)
        {
            DataRow row = table.Rows[i];
            if (int.Parse(row["SoLuong"].ToString()) == 0)
            {
                table.Rows.RemoveAt(i);
            }
            else
                i++;
        }
        string error = "Chú ý!\\r\\n";
        for (i = 0; i < arr.Count; i+=2)
        {
            error += "Sản phẩm:" + arr[i].ToString() + " có sô lượng vượt quá hiện tại là " + arr[i + 1].ToString() + " \\r\\n";
        }
        error += "Chúng tôi sẽ không cập nhật các sản phẩm này";
        if(arr.Count>0)
            Response.Write("<script>alert('"+error+"');location.href='"+Request.UrlReferrer.ToString()+"';</script>");
        else
            Response.Redirect(Request.UrlReferrer.ToString());
    }

    protected void btnTiepTuc_Click(object sender, EventArgs e)
    {
        Response.Redirect("../NguoiDung/NguoiDung_XemDanhSachSanPham.aspx");
    }
    protected void btnThanhToan_Click(object sender, EventArgs e)
    {
        Response.Redirect("KHTT_ThanhToan.aspx");
    }
}
